package xin.marcher.module.quality.cor.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import xin.marcher.module.quality.cor.RiskHandler;
import xin.marcher.module.quality.domain.dto.ItemQualityControlResultDTO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 风险数据处理链
 */
public class RiskHandlerChain implements RiskHandler {

    private static List<RiskHandler> handlerList = new ArrayList<>();

    private static RiskHandlerChain INSTANCE = new RiskHandlerChain();

    /**
     * 获取链条对象，自定义链条实现
     *
     * @param riskHandler
     * @return
     */
    public static RiskHandlerChain obtainRiskHandlerChain(RiskHandler... riskHandler) {
        handlerList.addAll(Arrays.asList(riskHandler));
        return INSTANCE;
    }

    private RiskHandlerChain() {
    }

    /**
     * 对从数据集市中拉取的风险结果数据依次进行处理
     *
     * @param riskDataMap
     * @return
     */
    @Override
    public Map<String, ItemQualityControlResultDTO> handle(Map<String, ItemQualityControlResultDTO> riskDataMap) {
        for (RiskHandler riskHandler : handlerList) {
            if (CollectionUtils.isEmpty(riskDataMap)) {
                return riskDataMap;
            }
            riskHandler.handle(riskDataMap);
        }
        return riskDataMap;
    }
}
